package com.zx.BookSeaReader.ui.chaptercomment;

import android.content.DialogInterface;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;

import com.zx.BookSeaReader.R;
import com.zx.BookSeaReader.base.BasePresenter;
import com.zx.BookSeaReader.common.APPCONST;
import com.zx.BookSeaReader.greendao.entity.Book;
import com.zx.BookSeaReader.greendao.entity.BookChapterComment;
import com.zx.BookSeaReader.greendao.entity.UserBean;
import com.zx.BookSeaReader.greendao.service.CommentService;
import com.zx.BookSeaReader.util.TextHelper;
import com.zx.BookSeaReader.widget.itemdecoration.DividerItemDecoration;

import java.util.List;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * @Author: ZX
 * @Time: 2020/4/25
 * @Description: 功能描述
 */
public class BookChapterCommentPresenter implements BasePresenter {

    private BookChapterCommentActivity mBookChapterCommentActivity;
    private CommentService mCommentService;
    private UserBean userBean;
    private Book book;
    private String chapterName;
    private List<BookChapterComment> ListBookComments;
    private List<BookChapterComment> ListBookHotComments;


    private BookChapterCommentAdapter mBookChapterCommentAdapter;
    private BookChapterHotCommentAdapter mBookChapterHotCommentAdapter;


    public BookChapterCommentPresenter(BookChapterCommentActivity bookChapterCommentActivity) {
        mBookChapterCommentActivity = bookChapterCommentActivity;
        mBookChapterCommentAdapter = new BookChapterCommentAdapter();
        mBookChapterHotCommentAdapter = new BookChapterHotCommentAdapter();
        mCommentService=new CommentService();
    }

    @Override
    public void start() {
        userBean = (UserBean)mBookChapterCommentActivity.getIntent().getSerializableExtra(APPCONST.USER);
        book = (Book)mBookChapterCommentActivity.getIntent().getSerializableExtra(APPCONST.BOOK);
        chapterName = mBookChapterCommentActivity.getIntent().getStringExtra(APPCONST.CHAPTER);
        TextHelper.showText(chapterName);
        mBookChapterCommentActivity.getRvHotComments().setLayoutManager(new LinearLayoutManager(mBookChapterCommentActivity));
        mBookChapterCommentActivity.getRvHotComments().addItemDecoration(new DividerItemDecoration(mBookChapterCommentActivity));
        mBookChapterCommentActivity.getRvHotComments().setAdapter(mBookChapterHotCommentAdapter);

        mBookChapterCommentActivity.getRvComments().setLayoutManager(new LinearLayoutManager(mBookChapterCommentActivity));
        mBookChapterCommentActivity.getRvComments().addItemDecoration(new DividerItemDecoration(mBookChapterCommentActivity));
        mBookChapterCommentActivity.getRvComments().setAdapter(mBookChapterCommentAdapter);
        mBookChapterCommentActivity.getTvChapterName().setText(chapterName);

        init();

    }
    public void initAdapter() {
        ListBookComments = mCommentService.getBookChapterComments(book);
        ListBookHotComments = mCommentService.getBookChapterHotComments(book);
        mBookChapterCommentAdapter.refreshItems(ListBookComments);
        mBookChapterHotCommentAdapter.refreshItems(ListBookHotComments);
        if(ListBookComments.size()<1){
            mBookChapterCommentActivity.getTvCommentsCount().setText("共有0条评论");

        }else{
            mBookChapterCommentActivity.getTvCommentsCount().setText("共有"+ListBookComments.size()+"条评论");
        }
    }
    public void init(){
        initAdapter();
        mBookChapterHotCommentAdapter.setOnItemLongClickListener(
                (v, pos) -> {
                    openItemDialog(mBookChapterHotCommentAdapter.getItem(pos));
                    return true;
                }
        );
        mBookChapterCommentAdapter.setOnItemLongClickListener(
                (v, pos) -> {
                    openItemDialog(mBookChapterCommentAdapter.getItem(pos));
                    return true;
                }
        );

        mBookChapterCommentActivity.getTvAddComment().setOnClickListener(view ->{
            addComment();
        });

    }

    /**
     * 添加评论
     * @param
     */
    private void addComment(){
        final EditText editText = new EditText(mBookChapterCommentActivity);
        AlertDialog.Builder editDialog = new AlertDialog.Builder(mBookChapterCommentActivity);
        //editDialog.setIcon(R.mipmap.ic_launcher_round);
        editDialog.setTitle("输入评论内容").setView(editText);
        editDialog.setPositiveButton("发送", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String content = editText.getText().toString();
                int size = ListBookComments.size()+1;
                mCommentService.addChapterComment(book,userBean,content,size,chapterName);
                TextHelper.showText("添加评论成功");
                initAdapter();
            }
        });
        editDialog.show();
    }

    private void openItemDialog(BookChapterComment chapterComment) {
        String[] menus;
        menus = mBookChapterCommentActivity.getResources().getStringArray(R.array.menu_long_comment_click);
        AlertDialog commentBeanDialog = new AlertDialog.Builder(mBookChapterCommentActivity)
                .setTitle(chapterComment.getContent())
                .setAdapter(new ArrayAdapter<String>(mBookChapterCommentActivity,
                                android.R.layout.simple_list_item_1, menus),
                        (dialog, which) -> onItemMenuClick(menus[which], chapterComment))
                .setNegativeButton(null, null)
                .setPositiveButton(null, null)
                .create();

        commentBeanDialog.show();
    }

    private void onItemMenuClick(String which, BookChapterComment chapterComment) {
        switch (which) {
            //点赞
            case "点赞":
                int likeCount = chapterComment.getLikeCount()+1;
                chapterComment.setLikeCount(likeCount);
                mCommentService.updateEntity(chapterComment);
                TextHelper.showText("点赞成功");
                initAdapter();

                break;
            //删除
            case "删除":
                if(userBean.getAdmin()!=1){
                    TextHelper.showLongText("你不是管理员无法删除评论");
                }else{
                    mCommentService.deleteEntity(chapterComment);
                    initAdapter();
                }
                break;
            default:
                break;
        }
    }

}
